
private float strokeWidth = 3.5f;    // 笔触宽度
private List<Double> volumeList;     // 分贝集合

public void setWaveform(double volume) {
    Log.i("ard", "分贝：" + volume);

    int height = getHeight();

    volume = volume * 100;
    volume = volume % height; // 不太精确的取差值

    if (volumeList.size() > 0) {
        this.volumeList.add(volumeList.size() - 1, volume);
    } else {
        this.volumeList.add(volume);
    }

    float volumeCountWidth = volumeList.size() * strokeWidth;
    int viewWidth = getWidth();
    if (volumeCountWidth > viewWidth) { // 如果全部绘制数据宽度超出了View的宽度就去除旧数据，达到滚动绘制效果
        do {
            volumeList.remove(0);
            volumeCountWidth = volumeList.size() * strokeWidth;
        } while (volumeCountWidth > viewWidth);
    }

    invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Path path = new Path();
    float height = getHeight();
    float width = getWidth();
    float size = volumeList.size();
    float volumeCountWidth = size * strokeWidth;
    float diffWidth = 0;
    if (volumeCountWidth < width) {
        diffWidth = width - volumeCountWidth; // 不想从view中心开始绘制-从左边开始，就注释掉
        diffWidth = diffWidth / 2;
    }

    for (int i = 0; i < size; i++) {
        if (0 == i % 2) {
            //continue; // 间隔绘制
        }

        double volume = volumeList.get(i);

        double top = height / 2 - volume;
        if (top < 0) {
            double tmp = volume;
            do {
                tmp = tmp / 4 * 3;
                top = height / 2 - tmp;
            } while (top < 0);
        }

        double bottom = height / 2 + volume;
        if (bottom > height) {
            double tmp = volume;
            do {
                tmp = tmp / 4 * 3;
                bottom = height / 2 + tmp;
            } while (bottom > height);
        }

        path.moveTo(i * strokeWidth + diffWidth, (float) (top));
        path.lineTo(i * strokeWidth + diffWidth, (float) (bottom));
    }

    canvas.drawPath(path, paint);
}
